import { Switch, Tooltip, message } from 'antd';
import pageStyles from '@/common/pageLayout.less';
import { ZRBGH_STATE, isShowPass, renderNeedURL } from '../common/Format';
import { QuestionCircleOutlined } from '@ant-design/icons';
import { useMemo } from 'react';
import { BZZ_CHECKSTATE, ColumnsRenderFun, DictionaryRenderFun, formatDecimal, getColorTextDoms } from '../../Acommon';
import { getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
const getSorter = (prev: Record<string, any>, next: Record<string, any>): number => (prev > next ? 1 : -1);
/**
 * 需求管理表格
 * */
const SyncTableColumns = (
  curRes: Record<string, any>,
  operateFuns: Record<string, any>,
  stateRes: string | number,
  needInfo: Record<string, any> | null,
  nodesFlowListMemo: Record<string, any> | boolean,
) => {
  const isAudit = useMemo(() => {
    return needInfo?.state && needInfo?.state == ZRBGH_STATE() && needInfo?.checkState == BZZ_CHECKSTATE() && nodesFlowListMemo;
  }, [needInfo?.state, needInfo?.checkState, nodesFlowListMemo]);

  return [
    {
      title: '标的类别',
      dataIndex: 'supplyTypeName',
      key: 'supplyTypeName',
      fixed: 'left',
      width: 135,
      render: (text: string, record: Record<string, any>) => DictionaryRenderFun({ text }),
    },
    {
      title: '标的名称',
      dataIndex: 'fullName',
      key: 'fullName',
      width: 165,
      visible: true,
      fixed: 'left',
      render: (text: string | null, record: Record<string, any>) => {
        // if (record?.supplyType != `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}1000`) return ColumnsRenderFun({ text });
        // return (
        //   <Tooltip title={text}>
        //     <div
        //       style={{ color: '#4d83f5', cursor: 'pointer' }}
        //       className={pageStyles['over-flow-hidden']}
        //       onClick={(e) => {
        //         e?.stopPropagation();
        //         operateFuns?.onView(record?.materialID);
        //       }}
        //     >
        //       {text || '--'}
        //     </div>
        //   </Tooltip>
        // );

        if (
          (record?.supplyType == `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}1003` ||
            record?.supplyType == `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}1004` ||
            record?.supplyType == `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}1000`) &&
          curRes?.['/materialController/details']
        ) {
          return (
            <Tooltip title={text}>
              <div
                style={{ color: '#4d83f5', cursor: 'pointer' }}
                className={pageStyles['over-flow-hidden']}
                onClick={(e) => {
                  e?.stopPropagation();
                  operateFuns?.onView(record?.materialID);
                }}
              >
                {text || '--'}
              </div>
            </Tooltip>
          );
        } else {
          return ColumnsRenderFun({ text });
        }
      },
      sorter: (a: any, b: any) => getSorter(a?.fullName, b?.fullName),
    },
    {
      title: '规格要求（参数）', // needParams[需求物料表中]  specInfo[物料表中]
      dataIndex: 'needParams',
      key: 'needParams',
      width: 180,
      visible: true,
      render: (text: string | null) => {
        return (
          <Tooltip>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
      sorter: (a: any, b: any) => getSorter(a?.needParams, b?.needParams),
    },

    {
      title: '预估单价（元）',
      dataIndex: 'expectPrice',
      key: 'expectPrice',
      width: 180,
      visible: true,
      render: (text: string | null) => {
        return (
          <Tooltip>
            <span className={pageStyles['over-flow-hidden']}>{formatDecimal(text)}</span>
          </Tooltip>
        );
      },
      sorter: (a: any, b: any) => getSorter(a?.expectPrice, b?.expectPrice),
    },
    {
      title: '需求数量（单位）',
      dataIndex: 'expectNum',
      key: 'expectNum',
      width: 180,
      visible: true,
      render: (text: string | null, record) => {
        return (
          <div style={{ display: 'flex' }} className={pageStyles['over-flow-hidden']}>
            <div style={{ marginRight: '8px' }}>{text || '--'}</div>
            {getColorTextDoms(record?.unitBusinessName)}
          </div>
        );
      },
      sorter: (a: any, b: any) => getSorter(a?.expectNum, b?.expectNum),
    },
    {
      title: '预估总价（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      width: 200,
      visible: true,
      render: (text: string | null) => {
        return (
          <Tooltip>
            <span className={pageStyles['over-flow-hidden']}>{formatDecimal(text)}</span>
          </Tooltip>
        );
      },
      sorter: (a: any, b: any) => getSorter(a?.expectSum, b?.expectSum),
    },
    {
      title: (
        <>
          <div>
            <span>审核状态</span>
            <a style={{ marginLeft: '3px' }}>
              <Tooltip title="单项金额未超过100万元则强制同意，不可更改！">
                <QuestionCircleOutlined />
              </Tooltip>
            </a>
          </div>
        </>
      ),
      dataIndex: 'checkState',
      key: 'checkState',
      width: 140,
      visible: true,
      render: (text: number) => {
        return (
          <Tooltip>
            <span className={pageStyles['over-flow-hidden']}>
              {!text ? (
                <span className={pageStyles['over-flow-hidden']} style={{ color: '#1677ff' }}>
                  待审核
                </span>
              ) : text == 1 ? (
                <span className={pageStyles['over-flow-hidden']} style={{ color: '#1e9392' }}>
                  同意
                </span>
              ) : text == 2 ? (
                <span className={pageStyles['over-flow-hidden']} style={{ color: 'red' }}>
                  不同意
                </span>
              ) : (
                '--'
              )}
            </span>
          </Tooltip>
        );
      },
      // sorter: (a: any, b: any) => getSorter(a?.checkState, b?.checkState),
    },
    {
      title: '计划采购时间',
      dataIndex: 'expectTime',
      key: 'expectTime',
      width: 190,
      visible: true,
      render: (text: string | null) => {
        return <span>{text ? text?.split(' ')?.[0] : '--'}</span>;
      },
      sorter: (a: any, b: any) => getSorter(a?.expectTime, b?.expectTime),
    },
    {
      title: '技术规范',
      dataIndex: 'skillNormURL',
      key: 'skillNormURL',
      width: 220,
      render: (text) => renderNeedURL(text),
    },
    {
      title: '调研报告',
      dataIndex: 'researchReportURL',
      key: 'researchReportURL',
      width: 220,
      render: (text) => renderNeedURL(text),
    },
    {
      title: '是否通过需求论证',
      dataIndex: 'needProofFlag',
      key: 'needProofFlag',
      width: 200,
      visible: true,
      render: (text: number, record) => {
        let isDis = Number(record?.expectSum) < 500000;
        if (!isDis) {
          return isShowPass(text);
        } else {
          return '/';
        }
      },
      sorter: (a: any, b: any) => getSorter(a?.needProofFlag, b?.needProofFlag),
    },
    {
      title: '需求论证意见',
      dataIndex: 'needProofURL',
      key: 'needProofURL',
      width: 200,
      visible: true,
      render: (text: string | null) => {
        return renderNeedURL(text);
      },
      sorter: (a: any, b: any) => getSorter(a?.needProofURL, b?.needProofURL),
    },
    {
      title: '是否通过进口专业人员论证',
      dataIndex: 'needImportFlag',
      key: 'needImportFlag',
      width: 220,
      visible: true,
      render: (text: number, record) => {
        // return <Tooltip>{isShowPass(text)}</Tooltip>;
        if (record?.importFlag == 1) {
          return isShowPass(text);
        } else {
          return '/';
        }
      },
      sorter: (a: any, b: any) => getSorter(a?.needImportFlag, b?.needImportFlag),
    },
    {
      title: '进口专业人员论证意见',
      dataIndex: 'needImportURL',
      key: 'needImportURL',
      width: 220,
      visible: true,
      render: (text: string | null) => {
        return renderNeedURL(text);
      },
      sorter: (a: any, b: any) => getSorter(a?.needImportURL, b?.needImportURL),
    },
    {
      title: '是否通过单一来源论证',
      dataIndex: 'needSingleFlag',
      key: 'needSingleFlag',
      width: 220,
      visible: true,
      render: (text: number, record) => {
        // return <Tooltip>{isShowPass(text)}</Tooltip>;
        if (record?.singleFlag == 1) {
          return isShowPass(text);
        } else {
          return '/';
        }
      },
      sorter: (a: any, b: any) => getSorter(a?.needSingleFlag, b?.needSingleFlag),
    },
    {
      title: '单一来源论证意见',
      dataIndex: 'needSingleURL',
      key: 'needSingleURL',
      width: 220,
      visible: true,
      render: (text: string | null) => {
        return renderNeedURL(text);
      },
      sorter: (a: any, b: any) => getSorter(a?.needSingleURL, b?.needSingleURL),
    },
    // {
    //   title: '相关附件',
    //   dataIndex: 'attachURL',
    //   key: 'attachURL',
    //   width: 200,
    //   visible: true,
    //   render: (text: string | null) => {
    //     return (
    //       <Tooltip>
    //         <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
    //       </Tooltip>
    //     );
    //   },
    //   sorter: (a: any, b: any) => getSorter(a?.attachURL, b?.attachURL),
    // },
    {
      title: '是否通过主任办公室审议',
      dataIndex: 'operation',
      key: 'operation',
      width: 230,
      visible: true,
      fixed: 'right',
      // align: 'center',
      render: (text: undefined, record: Record<string, any>) => {
        /**
         * 0=待审核  1=通过  2取消
         * */
        return (
          <div style={{}}>
            {/* <>
              <Tooltip title={'通过'}>
                <Button
                  icon={<CheckOutlined />}
                  type={'link'}
                  size={'small'}
                  className="manageBtntTxtStyle"
                  onClick={(e) => {
                    operateFuns?.onAudit(record, 'true');
                    e?.stopPropagation();
                  }}
                >
                  通过
                </Button>
              </Tooltip>
              <Tooltip title={'不通过'}>
                <Button
                  icon={<CloseOutlined />}
                  type={'link'}
                  size={'small'}
                  danger
                  className={Cls('manageBtntTxtStyle', 'manageColorRedBtn')}
                  onClick={(e) => {
                    operateFuns?.onAudit(record, 'false');
                    e?.stopPropagation();
                  }}
                >
                  不通过
                </Button>
              </Tooltip>
            </> */}
            {/* 是否通过主任办公室审议 */}
            <Switch
              checkedChildren="是"
              unCheckedChildren="否"
              defaultChecked
              checked={Number(record?.checkState) == 1 ? true : false}
              onChange={(value, e) => {
                if (Number(record?.expectSum) >= 1000000) {
                  e?.stopPropagation();
                  operateFuns?.onAudit(record, value + '');
                } else {
                  message?.warn('单项金额未超过100万元则强制同意，不可更改！');
                }
              }}
            />
          </div>
        );
      },
    },
  ]?.filter((item) => {
    if (curRes['/purchaseNeedMaterialController/check'] && isAudit) {
      return item;
    } else {
      return item?.key != 'operation';
    }
  });
};

export { SyncTableColumns };
